package com.cruise.game.controller.user;

import com.cruise.game.common.api.ApiResult;
import com.cruise.game.controller.BaseController;
import com.cruise.game.model.param.user.AgentApplicationParam;
import com.cruise.game.model.param.user.RechargeParam;
import com.cruise.game.model.param.user.UpdateProfileParam;
import com.cruise.game.model.param.user.WithdrawParam;
import com.cruise.game.model.vo.user.UserProfileVO;
import com.cruise.game.service.user.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户控制器
 * 
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/api/user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    /**
     * 获取用户个人信息
     */
    @ApiOperation("获取用户个人信息")
    @GetMapping("/profile")
    public ApiResult<UserProfileVO> getUserProfile() {
        return ApiResult.success(userService.getUserProfile(getCurrentUserId()));
    }

    /**
     * 更新用户个人信息
     */
    @ApiOperation("更新用户个人信息")
    @PutMapping("/profile")
    public ApiResult<UserProfileVO> updateUserProfile(@RequestBody UpdateProfileParam param) {
        return ApiResult.success("更新成功", userService.updateUserProfile(getCurrentUserId(), param));
    }

    /**
     * 申请成为打手
     */
    @ApiOperation("申请成为打手")
    @PostMapping("/apply-agent")
    public ApiResult<Map<String, Long>> applyAgent(@Validated @RequestBody AgentApplicationParam param) {
        Long applicationId = userService.applyAgent(getCurrentUserId(), param);
        Map<String, Long> result = new HashMap<>();
        result.put("applicationId", applicationId);
        return ApiResult.success("申请已提交，等待审核", result);
    }

    /**
     * 余额充值
     */
    @ApiOperation("余额充值")
    @PostMapping("/recharge")
    public ApiResult<Map<String, Object>> recharge(@Validated @RequestBody RechargeParam param) {
        UserService.PaymentInfoVO paymentInfo = userService.recharge(getCurrentUserId(), param);
        Map<String, Object> result = new HashMap<>();
        Map<String, String> paymentInfoMap = new HashMap<>();
        paymentInfoMap.put("appId", paymentInfo.getAppId());
        paymentInfoMap.put("timeStamp", paymentInfo.getTimeStamp());
        paymentInfoMap.put("nonceStr", paymentInfo.getNonceStr());
        paymentInfoMap.put("package", paymentInfo.getPackageValue());
        paymentInfoMap.put("signType", paymentInfo.getSignType());
        paymentInfoMap.put("paySign", paymentInfo.getPaySign());
        result.put("paymentInfo", paymentInfoMap);
        return ApiResult.success("充值请求已提交", result);
    }

    /**
     * 申请提现
     */
    @ApiOperation("申请提现")
    @PostMapping("/withdraw")
    public ApiResult<Map<String, Long>> withdraw(@Validated @RequestBody WithdrawParam param) {
        Long applicationId = userService.withdraw(getCurrentUserId(), param);
        Map<String, Long> result = new HashMap<>();
        result.put("applicationId", applicationId);
        return ApiResult.success("提现申请已提交", result);
    }
}
